package com.lulu.gateway;

import com.lulu.common.service.RouteRefreshService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * @author wlc
 * @version 1.0
 */
@Slf4j
@Service
public class RefreshServiceImpl implements RouteRefreshService {
    @Autowired
    private ApplicationEventPublisher publisher;
    @Autowired
    private MyRouteDefinitionRepository myRouteDefinitionRepository;

    @Override
    public boolean refresh() {
        try {
            myRouteDefinitionRepository.getRouteDefinitions();
            refreshRouteInfo();
        } catch (Exception e) {
            log.error("更新路由表时错误",e);
        }
        return true;
    }

    public void refreshRouteInfo(){
        publisher.publishEvent(new RefreshRoutesEvent(this));
    }
}
